﻿using Nett;
using System;
using System.Collections.Generic;
using System.IO;

namespace M2Server
{
    public class ExpConfig
    {
        private readonly TomlTable _tableConfig;
        private const string sConfigName = "config.toml";

        public ExpConfig()
        {
            if (!File.Exists(sConfigName))
            {
                Console.WriteLine($"配置文件{sConfigName}不存在.");
                return;
            }
            var input = new StreamReader(sConfigName);
            _tableConfig = Toml.ReadString(input.ReadToEnd());
        }

        public void LoadExp()
        {
            StartFixExp();
            M2Share.g_Config.dwKillMonExpMultiple = GetConfig<ushort>("Exp", "KillMonExpMultiple", M2Share.g_Config.dwKillMonExpMultiple);
            M2Share.g_Config.dwKillMonNGExpMultiple = GetConfig<ushort>("Exp", "KillMonNGExpMultiple", M2Share.g_Config.dwKillMonNGExpMultiple); // 杀怪内功经验倍数
            M2Share.g_Config.boHighLevelKillMonFixExp = GetConfig<bool>("Exp", "HighLevelKillMonFixExp", M2Share.g_Config.boHighLevelKillMonFixExp);

            for (var i = 0; i <= M2Share.MAXCHANGELEVEL - 1; i++)
            {
                M2Share.g_Config.dwNeedExps[i] = GetConfig<int>("Exp", "Level" + i, M2Share.g_Config.dwNeedExps[i]);
            }
            for (var i = 0; i <= M2Share.MAXCHANGELEVEL - 1; i++)
            {
                M2Share.g_Config.dwHeroNeedExps[i] = GetConfig<int>("HeroExp", "Level" + i, M2Share.g_Config.dwHeroNeedExps[i]);
            }

            // 药力值 20080623
            for (var i = 0; i < 50; i++)
            {
                M2Share.g_Config.dwMedicineNeedExps[i] = GetConfig<ushort>("MedicineExp", "Level" + i, M2Share.g_Config.dwMedicineNeedExps[i]);
            }

            // ----------------------------------------------------------------------------
            // 酒气护体 20080625
            for (var i = 0; i < 100; i++)
            {
                M2Share.g_Config.dwSkill68NeedExps[i] = GetConfig<int>("Skill68", "Level" + i, M2Share.g_Config.dwSkill68NeedExps[i]);
            }
            // 天地结晶经验
            for (var i = 0; i < 5; i++)
            {
                M2Share.g_Config.dwExpCrystalNeedExps[i] = GetConfig<int>("ExpCrystal", "Level" + i, M2Share.g_Config.dwExpCrystalNeedExps[i]);
            }
            // 天地结晶内功经验
            for (var i = 0; i < 5; i++)
            {
                M2Share.g_Config.dwNGExpCrystalNeedExps[i] = GetConfig<int>("NGExpCrystal", "Level" + i, M2Share.g_Config.dwNGExpCrystalNeedExps[i]);
            }
            M2Share.g_Config.boUseFixExp = GetConfig<bool>("Setup", "UseFixExp", M2Share.g_Config.boUseFixExp);
            M2Share.g_Config.nBaseExp = GetConfig<int>("Exp", "BaseExp", M2Share.g_Config.nBaseExp);
            M2Share.g_Config.nAddExp = GetConfig<int>("Setup", "AddExp", M2Share.g_Config.nAddExp);
        }

        public void StartFixExp()
        {
            M2Share.g_dwOldNeedExps = new int[M2Share.MAXCHANGELEVEL];
            M2Share.g_dwHeroNeedExps = new int[M2Share.MAXCHANGELEVEL];
            M2Share.g_dwMedicineExps = new ushort[50];
            M2Share.g_dwSkill68Exps = new int[110];
            M2Share.g_dwExpCrystalNeedExps = new int[5];
            M2Share.g_dwNGExpCrystalNeedExps = new int[5];
            M2Share.g_dwOldNeedExps[0] = 100;
            M2Share.g_dwOldNeedExps[2] = 200;
            M2Share.g_dwOldNeedExps[3] = 300;
            M2Share.g_dwOldNeedExps[4] = 400;
            M2Share.g_dwOldNeedExps[5] = 600;
            M2Share.g_dwOldNeedExps[6] = 900;
            M2Share.g_dwOldNeedExps[7] = 1200;
            M2Share.g_dwOldNeedExps[8] = 1700;
            M2Share.g_dwOldNeedExps[9] = 2500;
            M2Share.g_dwOldNeedExps[10] = 6000;
            M2Share.g_dwOldNeedExps[11] = 8000;
            M2Share.g_dwOldNeedExps[12] = 10000;
            M2Share.g_dwOldNeedExps[13] = 15000;
            M2Share.g_dwOldNeedExps[14] = 30000;
            M2Share.g_dwOldNeedExps[15] = 40000;
            M2Share.g_dwOldNeedExps[16] = 50000;
            M2Share.g_dwOldNeedExps[17] = 70000;
            M2Share.g_dwOldNeedExps[18] = 10000;
            M2Share.g_dwOldNeedExps[19] = 120000;
            M2Share.g_dwOldNeedExps[20] = 140000;
            M2Share.g_dwOldNeedExps[21] = 250000;
            M2Share.g_dwOldNeedExps[22] = 300000;
            M2Share.g_dwOldNeedExps[23] = 350000;
            M2Share.g_dwOldNeedExps[24] = 400000;
            M2Share.g_dwOldNeedExps[25] = 500000;
            M2Share.g_dwOldNeedExps[26] = 700000;
            M2Share.g_dwOldNeedExps[27] = 1000000;
            M2Share.g_dwOldNeedExps[28] = 1400000;
            M2Share.g_dwOldNeedExps[29] = 1800000;
            M2Share.g_dwOldNeedExps[30] = 2000000;
            M2Share.g_dwOldNeedExps[31] = 2400000;
            M2Share.g_dwOldNeedExps[32] = 2800000;
            M2Share.g_dwOldNeedExps[33] = 3200000;
            M2Share.g_dwOldNeedExps[34] = 3600000;
            M2Share.g_dwOldNeedExps[35] = 4000000;
            M2Share.g_dwOldNeedExps[36] = 4800000;
            M2Share.g_dwOldNeedExps[37] = 5600000;
            M2Share.g_dwOldNeedExps[38] = 8200000;
            M2Share.g_dwOldNeedExps[39] = 9000000;
            M2Share.g_dwOldNeedExps[40] = 12000000;
            M2Share.g_dwOldNeedExps[41] = 16000000;
            M2Share.g_dwOldNeedExps[42] = 30000000;
            M2Share.g_dwOldNeedExps[43] = 50000000;
            M2Share.g_dwOldNeedExps[44] = 80000000;
            M2Share.g_dwOldNeedExps[45] = 120000000;
            M2Share.g_dwOldNeedExps[46] = 480000000;
            M2Share.g_dwOldNeedExps[47] = 1000000000;
            M2Share.g_dwOldNeedExps[48] = 1500000000;
            M2Share.g_dwOldNeedExps[49] = 1800000000;
            for (var i = 50; i <= M2Share.g_dwOldNeedExps.GetUpperBound(0); i++)
            {
                M2Share.g_dwOldNeedExps[i] = 2000000000;
            }
            M2Share.g_dwHeroNeedExps[0] = 50;
            M2Share.g_dwHeroNeedExps[2] = 100;
            M2Share.g_dwHeroNeedExps[3] = 150;
            M2Share.g_dwHeroNeedExps[4] = 200;
            M2Share.g_dwHeroNeedExps[5] = 300;
            M2Share.g_dwHeroNeedExps[6] = 450;
            M2Share.g_dwHeroNeedExps[7] = 600;
            M2Share.g_dwHeroNeedExps[8] = 800;
            M2Share.g_dwHeroNeedExps[9] = 1200;
            M2Share.g_dwHeroNeedExps[10] = 3000;
            M2Share.g_dwHeroNeedExps[11] = 4000;
            M2Share.g_dwHeroNeedExps[12] = 5000;
            M2Share.g_dwHeroNeedExps[13] = 7500;
            M2Share.g_dwHeroNeedExps[14] = 15000;
            M2Share.g_dwHeroNeedExps[15] = 20000;
            M2Share.g_dwHeroNeedExps[16] = 25000;
            M2Share.g_dwHeroNeedExps[17] = 35000;
            M2Share.g_dwHeroNeedExps[18] = 50000;
            M2Share.g_dwHeroNeedExps[19] = 60000;
            M2Share.g_dwHeroNeedExps[20] = 70000;
            M2Share.g_dwHeroNeedExps[21] = 100000;
            M2Share.g_dwHeroNeedExps[22] = 150000;
            M2Share.g_dwHeroNeedExps[23] = 160000;
            M2Share.g_dwHeroNeedExps[24] = 200000;
            M2Share.g_dwHeroNeedExps[25] = 250000;
            M2Share.g_dwHeroNeedExps[26] = 350000;
            M2Share.g_dwHeroNeedExps[27] = 500000;
            M2Share.g_dwHeroNeedExps[28] = 700000;
            M2Share.g_dwHeroNeedExps[29] = 800000;
            M2Share.g_dwHeroNeedExps[30] = 1000000;
            M2Share.g_dwHeroNeedExps[31] = 1200000;
            M2Share.g_dwHeroNeedExps[32] = 1400000;
            M2Share.g_dwHeroNeedExps[33] = 1600000;
            M2Share.g_dwHeroNeedExps[34] = 1800000;
            M2Share.g_dwHeroNeedExps[35] = 2000000;
            M2Share.g_dwHeroNeedExps[36] = 2400000;
            M2Share.g_dwHeroNeedExps[37] = 2800000;
            M2Share.g_dwHeroNeedExps[38] = 4000000;
            M2Share.g_dwHeroNeedExps[39] = 4500000;
            M2Share.g_dwHeroNeedExps[40] = 6000000;
            M2Share.g_dwHeroNeedExps[41] = 8000000;
            M2Share.g_dwHeroNeedExps[42] = 15000000;
            M2Share.g_dwHeroNeedExps[43] = 25000000;
            M2Share.g_dwHeroNeedExps[44] = 40000000;
            M2Share.g_dwHeroNeedExps[45] = 60000000;
            M2Share.g_dwHeroNeedExps[46] = 240000000;
            M2Share.g_dwHeroNeedExps[47] = 500000000;
            M2Share.g_dwHeroNeedExps[48] = 800000000;
            M2Share.g_dwHeroNeedExps[49] = 900000000;
            for (var i = 50; i <= M2Share.g_dwHeroNeedExps.GetUpperBound(0); i++)
            {
                M2Share.g_dwHeroNeedExps[i] = 1000000000;
            }
            M2Share.g_dwMedicineExps[0] = 48;
            M2Share.g_dwMedicineExps[2] = 142;
            M2Share.g_dwMedicineExps[3] = 232;
            M2Share.g_dwMedicineExps[4] = 318;
            M2Share.g_dwMedicineExps[5] = 400;
            M2Share.g_dwMedicineExps[6] = 478;
            M2Share.g_dwMedicineExps[7] = 556;
            M2Share.g_dwMedicineExps[8] = 634;
            M2Share.g_dwMedicineExps[9] = 712;
            M2Share.g_dwMedicineExps[10] = 790;
            M2Share.g_dwMedicineExps[11] = 868;
            M2Share.g_dwMedicineExps[12] = 946;
            M2Share.g_dwMedicineExps[13] = 1024;
            M2Share.g_dwMedicineExps[14] = 1102;
            M2Share.g_dwMedicineExps[15] = 1180;
            M2Share.g_dwMedicineExps[16] = 1258;
            M2Share.g_dwMedicineExps[17] = 1336;
            M2Share.g_dwMedicineExps[18] = 1414;
            M2Share.g_dwMedicineExps[19] = 1492;
            M2Share.g_dwMedicineExps[20] = 1570;
            M2Share.g_dwMedicineExps[21] = 1648;
            M2Share.g_dwMedicineExps[22] = 1726;
            M2Share.g_dwMedicineExps[23] = 1804;
            M2Share.g_dwMedicineExps[24] = 1882;
            M2Share.g_dwMedicineExps[25] = 1960;
            M2Share.g_dwMedicineExps[26] = 2038;
            M2Share.g_dwMedicineExps[27] = 2116;
            M2Share.g_dwMedicineExps[28] = 2194;
            M2Share.g_dwMedicineExps[29] = 2272;
            M2Share.g_dwMedicineExps[30] = 2350;
            M2Share.g_dwMedicineExps[31] = 2428;
            M2Share.g_dwMedicineExps[32] = 2506;
            M2Share.g_dwMedicineExps[33] = 2584;
            M2Share.g_dwMedicineExps[34] = 2662;
            M2Share.g_dwMedicineExps[35] = 2740;
            M2Share.g_dwMedicineExps[36] = 2818;
            M2Share.g_dwMedicineExps[37] = 2896;
            M2Share.g_dwMedicineExps[38] = 2974;
            M2Share.g_dwMedicineExps[39] = 3052;
            M2Share.g_dwMedicineExps[40] = 3130;
            M2Share.g_dwMedicineExps[41] = 3208;
            M2Share.g_dwMedicineExps[42] = 3286;
            M2Share.g_dwMedicineExps[43] = 3364;
            M2Share.g_dwMedicineExps[44] = 3442;
            M2Share.g_dwMedicineExps[45] = 3520;
            M2Share.g_dwMedicineExps[46] = 3598;
            M2Share.g_dwMedicineExps[47] = 3676;
            M2Share.g_dwMedicineExps[48] = 3754;
            M2Share.g_dwMedicineExps[49] = 3832;
            for (var i = 50; i <= M2Share.g_dwMedicineExps.GetUpperBound(0); i++)
            {
                M2Share.g_dwMedicineExps[i] = 3910;
            }
            M2Share.g_dwSkill68Exps[0] = 3333000;
            M2Share.g_dwSkill68Exps[2] = 3816000;
            M2Share.g_dwSkill68Exps[3] = 4329000;
            M2Share.g_dwSkill68Exps[4] = 4872000;
            M2Share.g_dwSkill68Exps[5] = 5445000;
            M2Share.g_dwSkill68Exps[6] = 6048000;
            M2Share.g_dwSkill68Exps[7] = 6681000;
            M2Share.g_dwSkill68Exps[8] = 7344000;
            M2Share.g_dwSkill68Exps[9] = 8037000;
            M2Share.g_dwSkill68Exps[10] = 8760000;
            M2Share.g_dwSkill68Exps[11] = 9513000;
            M2Share.g_dwSkill68Exps[12] = 10296000;
            M2Share.g_dwSkill68Exps[13] = 11109000;
            M2Share.g_dwSkill68Exps[14] = 11952000;
            M2Share.g_dwSkill68Exps[15] = 12825000;
            M2Share.g_dwSkill68Exps[16] = 13728000;
            M2Share.g_dwSkill68Exps[17] = 14661000;
            M2Share.g_dwSkill68Exps[18] = 15624000;
            M2Share.g_dwSkill68Exps[19] = 16617000;
            M2Share.g_dwSkill68Exps[20] = 17640000;
            M2Share.g_dwSkill68Exps[21] = 18693000;
            M2Share.g_dwSkill68Exps[22] = 19776000;
            M2Share.g_dwSkill68Exps[23] = 20889000;
            M2Share.g_dwSkill68Exps[24] = 22032000;
            M2Share.g_dwSkill68Exps[25] = 23205000;
            M2Share.g_dwSkill68Exps[26] = 24408000;
            M2Share.g_dwSkill68Exps[27] = 25641000;
            M2Share.g_dwSkill68Exps[28] = 26904000;
            M2Share.g_dwSkill68Exps[29] = 28197000;
            M2Share.g_dwSkill68Exps[30] = 29520000;
            M2Share.g_dwSkill68Exps[31] = 30873000;
            M2Share.g_dwSkill68Exps[32] = 32256000;
            M2Share.g_dwSkill68Exps[33] = 33669000;
            M2Share.g_dwSkill68Exps[34] = 35112000;
            M2Share.g_dwSkill68Exps[35] = 36585000;
            M2Share.g_dwSkill68Exps[36] = 38088000;
            M2Share.g_dwSkill68Exps[37] = 39621000;
            M2Share.g_dwSkill68Exps[38] = 41184000;
            M2Share.g_dwSkill68Exps[39] = 42777000;
            M2Share.g_dwSkill68Exps[40] = 44400000;
            M2Share.g_dwSkill68Exps[41] = 46053000;
            M2Share.g_dwSkill68Exps[42] = 47736000;
            M2Share.g_dwSkill68Exps[43] = 49449000;
            M2Share.g_dwSkill68Exps[44] = 51192000;
            M2Share.g_dwSkill68Exps[45] = 52965000;
            M2Share.g_dwSkill68Exps[46] = 54768000;
            M2Share.g_dwSkill68Exps[47] = 56601000;
            M2Share.g_dwSkill68Exps[48] = 58464000;
            M2Share.g_dwSkill68Exps[49] = 60357000;
            M2Share.g_dwSkill68Exps[50] = 62280000;
            M2Share.g_dwSkill68Exps[51] = 64233000;
            M2Share.g_dwSkill68Exps[52] = 66216000;
            M2Share.g_dwSkill68Exps[53] = 68229000;
            M2Share.g_dwSkill68Exps[54] = 70272000;
            M2Share.g_dwSkill68Exps[55] = 72345000;
            M2Share.g_dwSkill68Exps[56] = 74448000;
            M2Share.g_dwSkill68Exps[57] = 76581000;
            M2Share.g_dwSkill68Exps[58] = 78744000;
            M2Share.g_dwSkill68Exps[59] = 80937000;
            M2Share.g_dwSkill68Exps[60] = 83160000;
            M2Share.g_dwSkill68Exps[61] = 85413000;
            M2Share.g_dwSkill68Exps[62] = 87696000;
            M2Share.g_dwSkill68Exps[63] = 90009000;
            M2Share.g_dwSkill68Exps[64] = 92352000;
            M2Share.g_dwSkill68Exps[65] = 94725000;
            M2Share.g_dwSkill68Exps[66] = 97128000;
            M2Share.g_dwSkill68Exps[67] = 99561000;
            M2Share.g_dwSkill68Exps[68] = 102024000;
            M2Share.g_dwSkill68Exps[69] = 104517000;
            M2Share.g_dwSkill68Exps[70] = 107040000;
            M2Share.g_dwSkill68Exps[71] = 109593000;
            M2Share.g_dwSkill68Exps[72] = 112176000;
            M2Share.g_dwSkill68Exps[73] = 114789000;
            M2Share.g_dwSkill68Exps[74] = 117432000;
            M2Share.g_dwSkill68Exps[75] = 120105000;
            M2Share.g_dwSkill68Exps[76] = 122808000;
            M2Share.g_dwSkill68Exps[77] = 125541000;
            M2Share.g_dwSkill68Exps[78] = 128304000;
            M2Share.g_dwSkill68Exps[79] = 131097000;
            M2Share.g_dwSkill68Exps[80] = 133920000;
            M2Share.g_dwSkill68Exps[81] = 136773000;
            M2Share.g_dwSkill68Exps[82] = 139656000;
            M2Share.g_dwSkill68Exps[83] = 142569000;
            M2Share.g_dwSkill68Exps[84] = 145512000;
            M2Share.g_dwSkill68Exps[85] = 148485000;
            M2Share.g_dwSkill68Exps[86] = 151488000;
            M2Share.g_dwSkill68Exps[87] = 154521000;
            M2Share.g_dwSkill68Exps[88] = 157584000;
            M2Share.g_dwSkill68Exps[89] = 160677000;
            M2Share.g_dwSkill68Exps[90] = 163800000;
            M2Share.g_dwSkill68Exps[91] = 166953000;
            M2Share.g_dwSkill68Exps[92] = 170136000;
            M2Share.g_dwSkill68Exps[93] = 173349000;
            M2Share.g_dwSkill68Exps[94] = 176592000;
            M2Share.g_dwSkill68Exps[95] = 179865000;
            M2Share.g_dwSkill68Exps[96] = 183168000;
            M2Share.g_dwSkill68Exps[97] = 186501000;
            M2Share.g_dwSkill68Exps[98] = 189864000;
            M2Share.g_dwSkill68Exps[99] = 193257000;
            M2Share.g_dwSkill68Exps[100] = 203257000;
            M2Share.g_dwExpCrystalNeedExps[0] = 300000;
            // 天地结晶升级经验
            M2Share.g_dwExpCrystalNeedExps[2] = 700000;
            M2Share.g_dwExpCrystalNeedExps[3] = 1200000;
            M2Share.g_dwExpCrystalNeedExps[4] = 1800000;
            M2Share.g_dwNGExpCrystalNeedExps[0] = 100000;
            // 天地结晶内功升级经验
            M2Share.g_dwNGExpCrystalNeedExps[2] = 240000;
            M2Share.g_dwNGExpCrystalNeedExps[3] = 400000;
            M2Share.g_dwNGExpCrystalNeedExps[4] = 580000;
        }

        public T GetConfig<T>(string nodeName, string keyName, object defaultValue)
        {
            if (!_tableConfig.ContainsKey(nodeName))
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T));
            }
            var yamlNode = _tableConfig.Get<Dictionary<string, object>>(nodeName);
            if (!yamlNode.TryGetValue(keyName, out object valuesData))
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T));
            }
            return (T)Convert.ChangeType(valuesData, typeof(T));
        }
    }
}
